/*
 * @Author: chen haoxuan
 * @Date: 2021-05-23 23:07:53
 * @LastEditTime: 2021-05-23 23:26:27
 */
// 4.7.1 重载流插入运算符 "<<"
/*
    用户自己定义的类型和数据（如类对象） 是不能直接用 "<<" 和 ">>"输出和输入的。如果想用它们输入和输出自己声明的类型的数据 必须对它们重载
    对 "<<" 和 ">>" 重载的函数形式如下
        istream & operator >> (istream &, 自定义类 &);
        ostream & operator >> (ostream &, 自定义类 &);
*/
#include<iostream>
using namespace std;
class Complex
{
    public:
        Complex(){real=0;imag=0;}
        Complex(double r,double i):real(r),imag(i){}
        Complex operator + (Complex &c2);
        friend ostream& operator <<(ostream &,Complex &);
    private:
        double real;
        double imag;
};
Complex Complex::operator+(Complex &c2)
{
    return Complex(real+c2.real,imag+c2.imag);
}
ostream & operator << (ostream & output,Complex &c)
{
    output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
    return output;
}
int main()
{
    Complex c1(2,4),c2(2,6),c3;
    c3=c1+c2;
    cout<<c3;
    system("pause");
    return 0;
}